package com.gz.hystrix.service.impl;

import com.gz.hystrix.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

@Service
public class PaymentServiceImpl implements PaymentService {
    public String getResultOk() {
        return "ok";
    }

    @HystrixCommand(fallbackMethod = "fallbackMethod", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public String getResultTimeOut() throws InterruptedException {
        Thread.sleep(5000);
        return "timeOut";
    }

    /**
     * 当在一个时间窗口（sleepWindowInMilliseconds）内请求数量达到requestVolumeThreshold（10）个会进行判断熔断与否，请求中错误请求的比例到达errorThresholdPercentage（60%）,
     * 就打开熔断器，一律将请求交给降级服务，打开状态有个时间（sleepWindowInMilliseconds）来限制，过了这个时间就会进入
     * 半熔断状态，此时允许一部分请求访问未降级服务，如果调用正常服务全都成功或者达到了一定比例
     * 就会恢复到关闭状态，否则认为还有问题，就回到打开状态，限制时间刷新重置
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallbackMethod", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")
    })
    public String getRongDMsg(int i) {
        if(i<0){
            throw new RuntimeException("i<0");
        }
        return null;
    }


    public String fallbackMethod() {
        return "程序异常.....";
    }
}
